package com.dmxh.service.impl;

import com.dmxh.mapper.TbItemCatMapper;
import com.dmxh.mapper.TbTypeTemplateMapper;
import com.dmxh.params.QueryParams;
import com.dmxh.pojo.TbItemCat;
import com.dmxh.pojo.TbItemCatExample;
import com.dmxh.pojo.TbTypeTemplate;
import com.dmxh.result.PageList;
import com.dmxh.service.ItemCatService;
import com.dmxh.vo.ItemCatVo;
import com.dmxh.vo.TypeTemplateVo;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class ItemCatServiceImpl implements ItemCatService {

    @Autowired
    private TbItemCatMapper tbItemCatMapper;
    @Autowired
    private TbTypeTemplateMapper tbTypeTemplateMapper;

    @Override
    public PageList<Map<String, Object>> queryPage(QueryParams queryParams) {
        Page<ItemCatVo> page = PageHelper.startPage(queryParams.getPageNum(), queryParams.getPageSize());
        TbItemCatExample tbItemCatExample = new TbItemCatExample();
        tbItemCatExample.createCriteria().andParentIdEqualTo(0L);
        List<TbItemCat> tbItemCats1 = tbItemCatMapper.selectByExample(tbItemCatExample);
        PageInfo<ItemCatVo> info = page.toPageInfo();
        List<TbItemCat> tbItemCats = tbItemCatMapper.selectByExample(null);

        List<Map<String, Object>> list = queryTree3(tbItemCats, 0);


//        List<ItemCatVo> collect = list.stream().map(tbItemCat -> {
//            ItemCatVo itemCatVo = new ItemCatVo();
//            BeanUtils.copyProperties(tbItemCat, itemCatVo);
//            return itemCatVo;
//        }).collect(Collectors.toList());


        return new PageList<Map<String, Object>>(info.getPageNum(), info.getPageSize(), info.getTotal(), list);
    }

    private List<Map<String, Object>> queryTree3(List<TbItemCat> tbItemCats, long i) {
        List<Map<String, Object>> list = new ArrayList<>();
        for (TbItemCat tbItemCat : tbItemCats) {
            if (tbItemCat.getParentId() == i) {
                HashMap<String, Object> map = new HashMap<>();
                map.put("id", tbItemCat.getId());
                map.put("name", tbItemCat.getName());
                map.put("typeId", tbItemCat.getTypeId());
                map.put("parentId", tbItemCat.getParentId());
                map.put("parentPath", tbItemCat.getParentPath());
                map.put("children", queryTree3(tbItemCats, tbItemCat.getId()));
                if (((List<Map<String, Object>>) map.get("children")).size() == 0) {
                    map.remove("children");
                }
                list.add(map);
            }
        }
        return list;
    }

    @Override
    public List<Map<String, Object>> queryTree() {
        List<TbItemCat> tbItemCats = tbItemCatMapper.selectByExample(null);
        return queryTree2(tbItemCats,0);
    }

    @Override
    public ItemCatVo queryById(long id) {
        TbItemCat tbItemCat = tbItemCatMapper.selectByPrimaryKey(id);
        ItemCatVo itemCatVo = new ItemCatVo();
        BeanUtils.copyProperties(tbItemCat, itemCatVo);
        return itemCatVo;
    }

    @Override
    public List<Map<String, Object>> queryTreeById(long id) {
        TbItemCat tbItemCat = tbItemCatMapper.selectByPrimaryKey(id);
        List<TbItemCat> tbItemCats = tbItemCatMapper.selectByExample(null);
//        TbItemCatExample tbItemCatExample = new TbItemCatExample();
//        tbItemCatExample.createCriteria().and
        return queryTreeById2(tbItemCats,tbItemCat.getId());
    }

    @Override
    public void addInfo(ItemCatVo itemCatVo) {
        TbItemCat tbItemCat = new TbItemCat();
        BeanUtils.copyProperties(itemCatVo, tbItemCat);
        if (tbItemCat.getParentId() == 0) {
            tbItemCat.setParentPath(null);
            tbItemCatMapper.insertSelective(tbItemCat);
            return;
        }
        TbItemCatExample tbItemCatExample = new TbItemCatExample();
        tbItemCatExample.createCriteria().andIdEqualTo(tbItemCat.getParentId());
        List<TbItemCat> tbItemCats = tbItemCatMapper.selectByExample(tbItemCatExample);
        TbItemCat tbItemCat1 = tbItemCats.get(0);
        tbItemCat.setParentPath(tbItemCat1.getParentPath());
        tbItemCatMapper.insertSelective(tbItemCat);
    }

    @Override
    public void updateInfo(ItemCatVo itemCatVo) {
        TbItemCat tbItemCat = new TbItemCat();
        BeanUtils.copyProperties(itemCatVo, tbItemCat);
        if (tbItemCat.getParentId() == 0) {
            tbItemCat.setParentPath(null);
            tbItemCatMapper.updateByPrimaryKeySelective(tbItemCat);
            return;
        }
        TbItemCatExample tbItemCatExample = new TbItemCatExample();
        tbItemCatExample.createCriteria().andIdEqualTo(tbItemCat.getParentId());
        List<TbItemCat> tbItemCats = tbItemCatMapper.selectByExample(tbItemCatExample);
        TbItemCat tbItemCat1 = tbItemCats.get(0);
        tbItemCat.setParentPath(tbItemCat1.getParentPath());
        tbItemCatMapper.updateByPrimaryKeySelective(tbItemCat);

    }

    @Override
    public void deleteInfo(long id) {
        tbItemCatMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void deleteBatch(Integer ids) {
        List<Integer> list = Arrays.asList(ids);
        TbItemCatExample tbItemCatExample = new TbItemCatExample();
        List<Long> collect = list.stream().map(Long::valueOf).collect(Collectors.toList());
        tbItemCatExample.createCriteria().andIdIn(collect);
        tbItemCatMapper.deleteByExample(tbItemCatExample);
    }

    @Override
    public TypeTemplateVo queryBrandById(long id) {
        TbItemCat tbItemCat = tbItemCatMapper.selectByPrimaryKey(id);
        TbTypeTemplate tbTypeTemplate = tbTypeTemplateMapper.selectByPrimaryKey(tbItemCat.getTypeId());
        TypeTemplateVo typeTemplateVo = new TypeTemplateVo();
        BeanUtils.copyProperties(tbTypeTemplate,typeTemplateVo);
        return typeTemplateVo;
    }

    private List<Map<String, Object>> queryTreeById2(List<TbItemCat> tbItemCats, Long id) {
        List<Map<String, Object>> list = new ArrayList<>();
        for (TbItemCat tbItemCat : tbItemCats) {
            if (tbItemCat.getParentId() == id) {
                HashMap<String, Object> map = new HashMap<>();
                map.put("value", tbItemCat.getId());
                map.put("label", tbItemCat.getName());
                map.put("children", queryTree2(tbItemCats, tbItemCat.getId()));
                if (((List<Map<String, Object>>) map.get("children")).size() == 0) {
                    map.remove("children");
                }
                list.add(map);
            }
        }
        return list;
    }


    private List<Map<String, Object>> queryTree2(List<TbItemCat> tbItemCats, long i) {
        List<Map<String, Object>> list = new ArrayList<>();
        for (TbItemCat tbItemCat : tbItemCats) {
            if (tbItemCat.getParentId() == i) {
                HashMap<String, Object> map = new HashMap<>();
                map.put("value", tbItemCat.getId());
                map.put("label", tbItemCat.getName());
                map.put("children", queryTree2(tbItemCats, tbItemCat.getId()));
                if (((List<Map<String, Object>>) map.get("children")).size() == 0) {
                    map.remove("children");
                }
                list.add(map);
            }
        }
        return list;
    }
}
